// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router.js'
import 'babel-polyfill'
import 'es6-promise/auto'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'




//引入store
import { store } from './store/index'
//引入element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

//MD5加密
// import crypto from 'crypto'
// Vue.prototype.crypto = crypto
// 引入awesome
import vueAwesome from 'vue-awesome'
import Icon from '../src/components/Icon.vue';
import './Icons/index.js';
Vue.use(vueAwesome)
Vue.component('icon', Icon);


//引入省市区联动插件
import VueAreaLinkage from 'vue-area-linkage';
import 'vue-area-linkage/dist/index.css'
Vue.use(VueAreaLinkage)


//引入axios
import axios from 'axios'

import {_http} from './axios/http'
// 也可以像下面这样，区分环境或者区分服务器等等

Vue.prototype.$http = _http;

import MenuUtils from './utils/MenuUtils';
window.sessionStorage.setItem('title',process.env.TITLE)
let data = JSON.parse(window.sessionStorage.getItem('user'));

// if (data){
//   //这里是防止用户手动刷新页面，整个app要重新加载,动态新增的路由，会消失，所以我们重新add一次
//   let routes = []
//   MenuUtils(routes,data)
//
//   router.addRoutes(routes)
//   window.sessionStorage.removeItem('isLoadNodes')
// }
if(store.state.routes.routes.length>0){

}else{
  store.state.routes.routes = JSON.parse(window.sessionStorage.getItem('routeConst'))
}
router.beforeEach((route, redirect, next) => {
  NProgress.start();

  window.sessionStorage.setItem('beforeRoute',redirect.path.split('/')[1]);
  window.sessionStorage.setItem('currentRoute',route.path.split('/')[1]);
  store.state.user.tags = unique(store.state.user.tags) //跳转前去重，防止关闭其他按钮闪现
  let data = JSON.parse(window.sessionStorage.getItem('user'));

  if(data&&route.path === '/login'){
    //这里不使用router进行跳转，是因为，跳转到登录页面的时候，是需要重新登录，获取数据的，这个时候，会再次向router实例里面add路由规则，
    //而next()跳转过去之后，没有刷新页面，之前的规则还是存在的，但是使用location的话，可以刷新页面，当刷新页面的时候，整个app会重新加载
    //而我们在刷新之前已经把sessionStorage里的user移除了，所以上面的添加路由也不行执行

    window.sessionStorage.removeItem('user');
    window.sessionStorage.removeItem('isLoadNodes');
    window.sessionStorage.removeItem('routeConst')
    window.location.href = '/';
    document.title = '美途在线-管理中心'

    return false
  }
  // if (!data && route.path !== '/login') {
  //   next({ path: '/login' })
  //   document.title = '配资系统'
  // } else {
  //   if (route.path) {
  //     next()
  //     document.title = route.name
  //   } else {
  //     next({ path: '/404' })
  //   }
  // }
  // console.log(!JSON.parse(window.sessionStorage.getItem('routeConst')))
  if(route.path!== '/login'&&route.path!== '/'){
    if(!window.sessionStorage.getItem('routeConst')){
      next('/login')
    }
  }
  if (route.matched.length ===0) {
    console.log(77)                                        //如果未匹配到路由
    redirect.name ? next({ name:redirect.name }) : next('/login');   //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  } else {
    next();                                                                            //如果匹配到正确跳转
  }

})
router.afterEach(transition => {
  NProgress.done();
  var obj = {};
  // console.log(router.options);
  var rout = JSON.parse(window.sessionStorage.getItem('user'));

  // if(store.state.user.tags.length == 0){
  //   if(transition.path!= '/login' ||transition.name != ""){
  //     store.state.user.tags.push({"name":transition.name,"path":transition.path});
  //     store.state.user.tags = unique(store.state.user.tags)
  //     return
  //   }
  //
  // }
  for(var key in rout){
    if(rout[key].children){
      for(var jey in rout[key].children){
        if(rout[key].children[jey].path == transition.path.substr(1)){
          if(transition.path!= '/login' ||transition.name != ""){
            if( transition.path.indexOf('Detail')<0){
              store.state.user.tags.push({"name":transition.name,"path":transition.path});
              store.state.user.tags = unique(store.state.user.tags)
              return
            }

          }
        }}
    }
  }



});
Vue.config.productionTip = false;
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store:store,
  template: '<App/>',
  components: { App }
})
function unique(songs){
  let result = {};
  let finalResult=[];
  for(let i=0;i<songs.length;i++){
    result[songs[i].name]=songs[i];
  }
  for(var item in result){
    finalResult.push(result[item]);
  }

  return finalResult;
}

